﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Globalization;


namespace hozoroghiab
{
    public partial class Form1 : Form
    {
        public sql sqlquery;
        public Form1(sql s)
        {
            InitializeComponent();
            sqlquery = s;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void importDataBaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            op.ShowDialog();
            if (op.FileName != "")
            {
                StreamReader sr = new StreamReader(op.FileName);

                sqlquery.open();
                while (!sr.EndOfStream)
                {
                    string msg = sr.ReadLine();
                    string[] data = msg.Split(' ');
                    if (data.Length == 6)
                        sqlquery.ExecuteNonQuery(@"if not exists(SELECT * FROM entry where employeid=" + data[0] + " and day=" + data[1].Substring(4, 2) + " and month=" + data[1].Substring(2, 2) + " and year=13" + data[1].Substring(0, 2) + " and hour=" + data[2].Substring(0, 2) + " and minute=" + data[2].Substring(2, 2) + " and code=" + data[3] + " and door=" + data[4] + ") INSERT INTO entry (employeid,year,month,day,hour,minute,code,door) VALUES(" + data[0] + ",13" + data[1].Substring(0, 2) + "," + data[1].Substring(2, 2) + "," + data[1].Substring(4, 2) + "," + data[2].Substring(0, 2) + "," + data[2].Substring(2, 2) + "," + data[3] + "," + data[4] + ")");
                    else
                        MessageBox.Show("file format is not correct");
                }
                sqlquery.close();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
           // MessageBox.Show("حمید رضا عبدی و میلاد بادران", "تهیه و تنظیم");
            AboutBox1 ab = new AboutBox1();
            ab.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            settime();
            System.Threading.Thread tr = new System.Threading.Thread(settime);
            PersianCalendar per = new PersianCalendar();
            per.GetMonth(DateTime.Now);
            toolStripcurrentdate.Text = per.GetYear(DateTime.Now) + "/" + per.GetMonth(DateTime.Now) + "/" + per.GetDayOfMonth(DateTime.Now);

        }

        private void creatBackupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "bakup files (*.bak)|*.bak|All files (*.*)|*.*";
            sfd.ShowDialog();
            if (sfd.FileName != "")
            {

                sqlquery.open();
                sqlquery.ExecuteNonQuery(@"backup database absent TO DISK = '" + sfd.FileName + "'");
                sqlquery.close();
            }
        }

        private void restorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "bakup files (*.bak)|*.bak|All files (*.*)|*.*";
            op.ShowDialog();

            if (op.FileName != "")
            {
                try
                {
                    SqlConnection myConnection = new SqlConnection("Data Source=HAMIDREZA-PC;Initial Catalog=master;Integrated Security=True");
                    myConnection.Open();
                    SqlCommand myCommand = new SqlCommand(@"RESTORE Database absent FROM DISK = '" + op.FileName + "' with REPLACE", myConnection);
                    myConnection.Close();
                }
                catch
                {
                    MessageBox.Show("error");
                }
            }
        }

        private void creatUserToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }



        private void نمایشکاربرانToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editestaff edstaff = new editestaff(sqlquery);
            edstaff.Show();
        }

        private void تعطیلاتToolStripMenuItem_Click(object sender, EventArgs e)
        {
            holidays hform = new holidays(sqlquery);
            hform.Show();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void naghesiaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            naghesia ngh = new naghesia(sqlquery);
            ngh.Show();
        }

        private void absentOfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            absentofhour ab = new absentofhour(sqlquery);
            ab.Show();

        }


        private void absentOfDayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            absentofday abs = new absentofday(sqlquery);
            abs.Show();
        }

        private void مرخصیToolStripMenuItem_Click(object sender, EventArgs e)
        {
            release re = new release(sqlquery);
            re.Show();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void ماموریتهاToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mission msform = new mission(sqlquery);
            msform.Show();
        }

        private void usersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            user_tabs add = new user_tabs(sqlquery);
            add.Show();
        }

        private void مدیریتکاربرانToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editestaff edit = new editestaff(sqlquery);
            edit.Show();
        }

        private void ماشینحسابToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc");
        }

        void settime()
        {
            timetoolStripStatusLabel3.Text = DateTime.Now.ToString("H:MM");
            System.Threading.Thread.Sleep(1000);
        }




    }
}
